# -*- encoding=utf8 -*-
__author__ = "yangxianming"

from airtest.core.api import *
from airtest.cli.parser import cli_setup
# ST.PROJECT_ROOT = "E:\AirtestWorkSpace\fuyuanwaiUItest"
# using(r"E:\AirtestWorkSpace\fuyuanwaiUItest\common.air")
# from common import *
    
auto_setup(__file__)

#拿到设备编号
import sys
args = sys.argv
devicePid = args[4].split("/")[3]
print("--------" + devicePid)
rootArr = args[2].split("\\")
scriptRoot = ""
for i in range(len(rootArr) - 1):
    scriptRoot = scriptRoot + rootArr[i] + "\\"
print("--------" + scriptRoot)

#对更目录下设变编号文件做注册登记操作 1代表在使用  0代表停止使用
with open(scriptRoot + devicePid, 'w') as f:
    f.write('1')
    
using(scriptRoot + "common.air")
from common import *
stop_app("com.alibaba.android.rimet")
start_app("com.alibaba.android.rimet")

try:
    
    sleep(1.0)
    openKoufu()
    sleep(2.0)
    openScene("dingdongmaicai")
    ##叮咚买菜加载较慢，先停一会等加载
    sleep(8.0)
    wait(Template(r"tpl1680073704638.png", record_pos=(-0.001, -0.895), resolution=(1080, 2244)))
    ##点击搜索
    touch(Template(r"tpl1680270371265.png",target_pos=4, record_pos=(0.243, -0.513), resolution=(1080, 2244)))
    
    sleep(2.0)
    #输入苹果
    text("苹果")
    ##可能弹出样式切换提示，点知道了
    sleep(2.0)
    pIknow = Template(r"tpl1680075237178.png", resolution=(1080, 2244))
    if exists(pIknow):
        touch(pIknow)
    # 点第一个苹果的购物车图标
    touch(Template(r"tpl1680491898852.png", rgb=True, record_pos=(-0.068, 0.527), resolution=(1080, 1920)))
    
    #点上方的购物车
    touch(Template(r"tpl1680075575350.png", record_pos=(0.346, -0.787), resolution=(1080, 2244)))
    sleep(2.0)
    #点去结算
    touch(Template(r"tpl1680076304124.png", record_pos=(0.31, 0.834), resolution=(1080, 2244)))
    sleep(3.0)
    #点选择收获地址
    wait(Template(r"tpl1680490367237.png"))
    touch(Template(r"tpl1680490367237.png", record_pos=(-0.003, -0.465), resolution=(1080, 1920)))
    sleep(3.0)
    #点地址
    touch(Template(r"tpl1680075666195.png", record_pos=(-0.323, -0.774), resolution=(1080, 2244)))
    sleep(1.0)
    #点送达时间
    touch(Template(r"tpl1680076201610.png", record_pos=(0.279, -0.444), resolution=(1080, 2244)))
    sleep(2.0)
    #点明天，点明天的第一条
    touch(Template(r"tpl1680075896731.png", record_pos=(-0.433, 0.11), resolution=(1080, 2244)))
    touch(Template(r"tpl1680270881497.png", record_pos=(-0.044, -0.172), resolution=(1080, 1920)))
    
    sleep(1.0)
    #点立即支付
    touch(Template(r"tpl1680271092060.png", record_pos=(0.318, 0.819), resolution=(1080, 1920)))
    
    sleep(5.0)
    #返回我们界面验证支付按钮是否正常
    assert_exists(Template(r"tpl1682042396544.png", record_pos=(-0.271, 0.353), resolution=(1080, 1920)), "请填写测试点")


except Exception as e:
    print(e)
finally:
    sleep(1.0)
    stop_app("com.alibaba.android.rimet")
    #完成测试等级设备为0以便被其他脚本使用
    with open(scriptRoot + devicePid, 'w') as f:
        f.write('0')

